#include <bits/stdc++.h>
using namespace std;

int n, m, ap[100007], a[100007], c[100007], d[30][100007], T, X[260][4];

int main(){
	freopen("arena.in", "r", stdin);
	freopen("arena.out", "w", stdout);
	scanf("%d%d", &n, &m);
	for(int i = 1; i <= n; i++) scanf("%d", &ap[i]);
	for(int i = 1; i <= m; i++) scanf("%d", &c[i]);
	int z1 = 1, K = 1;
	while(z1 < n) z1 <<= 1, K++;
	for(int i = K; i >= 1; i--){
		char c;
		for(int j = 1; j <= z1; j++){
			scanf("%c", &c);
			d[i][j] = c - '0';
		}
		z1 >>= 1;
	}
	scanf("%d", &T);
	for(int i = 1; i <= T; i++){
		scanf("%d%d%d%d", &X[i][0], &X[i][1], &X[i][2], &X[i][3]);
		for(int j = 1; j <= n; j++)
			a[j] = ap[j] ^ X[i][j % 4];
		long long ans = 0;
		for(int j = 1; j <= m; j++){
			int res = 0, rsid = 0;
			for(int q = 1; q <= c[j]; q++){
				if(a[q] > res){
					res = a[q];
					rsid = q;
				}
			}
			ans ^= rsid;
		}
		printf("%lld\n", ans);
	}
	return 0;
}
